﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Diagnostics.Contracts;
using RaisingForce.Raise.Hardware.Rules;

namespace RaisingForce.Raise.Hardware
{
    public static class IRobotHardwareOptionsExtensions
    {
        #region Extension Methods

        public static void GetWheelsPower(
            float wheelBalance,
            sbyte supposedPower,
            out sbyte leftWheelPower,
            out sbyte rightWheelPower)
        {
            if (!RobotHardwareOptionsRules.BalanceIsValid(wheelBalance))
            {
                throw new ArgumentException("wheelBalance");
            }
            if (!RobotHardwareOptionsRules.PowerIsValid(supposedPower))
            {
                throw new ArgumentException("supposedPower");
            }
            Contract.Ensures(RobotHardwareOptionsRules.PowerIsValid(Contract.ValueAtReturn<sbyte>(out leftWheelPower)));
            Contract.Ensures(RobotHardwareOptionsRules.PowerIsValid(Contract.ValueAtReturn<sbyte>(out rightWheelPower)));
            Contract.EndContractBlock();
            sbyte leftPower = supposedPower;
            float rightPower = leftPower * wheelBalance;
            if (rightPower <= 100)
            {
                leftWheelPower = leftPower;
                rightWheelPower = (sbyte)Math.Floor(rightPower);
            }
            else
            {
                leftWheelPower = (sbyte)Math.Floor(100 / wheelBalance);
                rightWheelPower = 100;
            }
        }

        public static void GetWheelsPower(
            this IRobotHardwareOptions options,
            out sbyte leftWheelPower,
            out sbyte rightWheelPower)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            Contract.Ensures(RobotHardwareOptionsRules.PowerIsValid(Contract.ValueAtReturn<sbyte>(out leftWheelPower)));
            Contract.Ensures(RobotHardwareOptionsRules.PowerIsValid(Contract.ValueAtReturn<sbyte>(out rightWheelPower)));
            Contract.EndContractBlock();
            IRobotHardwareOptionsExtensions.GetWheelsPower(
                options.WheelBalance,
                options.SupposedPower,
                out leftWheelPower,
                out rightWheelPower);
        }

        public static bool Equals(
            IRobotHardwareOptions options,
            IRobotHardwareOptions other)
        {
            if (object.ReferenceEquals(options, other))
            {
                return true;
            }
            if (options == null || other == null)
            {
                return false;
            }
            return options.ComPort == other.ComPort
                && options.WheelBalance == other.WheelBalance
                && options.LeftTurnTacho == other.LeftTurnTacho
                && options.RightTurnTacho == other.RightTurnTacho
                && options.SupposedPower == other.SupposedPower
                && options.ObstacleDistance.Equals(other.ObstacleDistance)
                && options.WheelRunDistance.Equals(other.WheelRunDistance)
                && options.ObstacleDetectionTime.Equals(other.ObstacleDetectionTime)
                && options.WheelRunTime.Equals(other.WheelRunTime)
                && options.LeftTurnTime.Equals(other.LeftTurnTime)
                && options.RightTurnTime.Equals(other.RightTurnTime);
        }

        public static int GetHashCode(IRobotHardwareOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            Contract.EndContractBlock();
            return options.ComPort.GetHashCode()
                ^ options.WheelBalance.GetHashCode()
                ^ options.LeftTurnTacho.GetHashCode()
                ^ options.RightTurnTacho.GetHashCode()
                ^ options.SupposedPower.GetHashCode()
                ^ options.ObstacleDistance.GetHashCode()
                ^ options.WheelRunDistance.GetHashCode()
                ^ options.ObstacleDetectionTime.GetHashCode()
                ^ options.WheelRunTime.GetHashCode()
                ^ options.LeftTurnTime.GetHashCode()
                ^ options.RightTurnTime.GetHashCode();
        }

        #endregion
    }
}
